package nl.sanity.managers 
{
	import flash.system.ApplicationDomain;
	import flash.text.Font;
	
	/**
	 * FontManager
	 *
	 * @langversion ActionScript 3
	 *
	 * @author A. Gosman
	 * @since  28/03/2009
	 */
	public class FontManager 
	{
		protected static var INSTANCE		: FontManager;
		
		protected var _applicationDomain 	: ApplicationDomain;
		
		// --- CONSTRUCTOR ----------------------------------------

		public function FontManager ( caller:Function = null ) 
		{
			if ( caller != FontManager.getInstance )
			{
                throw new Error ( "FontManager is a singleton class, use getInstance() instead" );
			}
           
			if ( FontManager.INSTANCE != null )
			{
                throw new Error( "Only one FontManager instance should be instantiated" );	
			}
			
			INSTANCE = this;
		}
		
		// --- PUBLIC FUNCTIONS -------------------------------------------------------------
		
		public static function getInstance():FontManager
		{
			return INSTANCE || new FontManager ( arguments.callee );
		}
		
		public function setApplicationDomain( applicationDomain: ApplicationDomain ):void
		{
			_applicationDomain		= applicationDomain;
		}
		
		public function registerFonts( fontList:Array ):void 
		{
			for ( var i:uint = 0; i < fontList.length; i++ ) 
			{
				Font.registerFont( getFontClass( fontList[ i ] ) );
			}
		}
		
		public function getFontClass( id:String ):Class 
		{
			if ( !_applicationDomain )
			{
				throw new Error( "ApplicationDomain is not defined" );
			}
			
			return _applicationDomain.getDefinition( id ) as Class;
		}

		public function getFont( id:String ):Font 
		{
			return getFontClass( id ) as Font;
		}
		
	}
}
